package beautiful.butterfly.schedule.test;

import beautiful.butterfly.schedule.MultipleTaskDeal;
import beautiful.butterfly.schedule.TaskItemAndParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class DBDemoMultipleTaskDeal implements MultipleTaskDeal<Long> {

    private static transient Logger log = LoggerFactory.getLogger(DBDemoMultipleTaskDeal.class);

    protected DataSource dataSource;

    public Comparator<Long> getComparator() {
        return new Comparator<Long>() {
            public int compare(Long o1, Long o2) {
                return o1.compareTo(o2);
            }

            public boolean equals(Object obj) {
                return this == obj;
            }
        };
    }

    public List<Long> getTaskList(String taskParameter, String ownSign, int taskItemNum,
                                  int fetchNum, List<TaskItemAndParameter> taskItemAndParameterList) throws Exception {
        List<Long> list = new ArrayList<Long>();
        if (taskItemAndParameterList.size() == 0) {
            return list;
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < taskItemAndParameterList.size(); i++) {
            if (i > 0) {
                sb.append(",");
            }
            sb.append(taskItemAndParameterList.get(i).getTaskItem());
        }

        Connection connection = null;
        try {
            connection = dataSource.getConnection();

            String sql = "select id from schedule_test where" +//
                    " own_sign = '" + ownSign + "'  " +//
                    "and mod(id," + taskItemNum + ") in (" + sb.toString() + ") " +//
                    "and status ='n' LIMIT " + fetchNum;//
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                list.add(resultSet.getLong("id"));
            }
            resultSet.close();
            preparedStatement.close();
            return list;
        } finally {
            if (connection != null)
                connection.close();
        }
    }


    public boolean execute(Long[] tasks, String ownSign) throws Exception {
        Connection connection = null;
        long id = 0;
        try {
            connection = dataSource.getConnection();
            for (int index = 0; index < tasks.length; index++) {
                id = (tasks[index]).longValue();

                String sql = "update schedule_test set status ='y' ,deal_count = deal_count + 1 where id = ? and status ='n' ";
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setLong(1, id);
                preparedStatement.executeUpdate();
                preparedStatement.close();
            }
            connection.commit();
        } catch (Exception e) {
            log.error("" + id + e.getMessage(), e);
            if (connection != null) {
                connection.rollback();
            }
            return false;
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
        return true;
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }


}